﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Pap.Data;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Reports.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class TotalSystemReportViewModel : IViewModel
    {
        /// <summary>
        /// 
        /// </summary>
        IReportRepository _ReportRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="TotalSystemReportViewModel"/> class.
        /// </summary>
        /// <param name="repo">The repo.</param>
        public TotalSystemReportViewModel(IReportRepository repo)
        {
            _ReportRepository = repo;
        }

        /// <summary>
        /// Gets all projects.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<IProject> GetAllProjects()
        {
            return _ReportRepository.FindProjects(project => true);
        }

        /// <summary>
        /// Gets all educational levels.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ILookUpEntity> GetAllEducationalLevels()
        {
            return _ReportRepository.FindEducationalLevels(project => true);
        }

        /// <summary>
        /// Gets all committee.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ICommittee> GetAllCommittee()
        {
            return _ReportRepository.FindCommittee(committee => true);
        }

        /// <summary>
        /// Gets all social statuses.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ILookUpEntity> GetAllSocialStatuses()
        {
            return _ReportRepository.FindSocialStatuses(socialStatus => true);
        }

        /// <summary>
        /// Gets all project stages.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<IProjectStage> GetAllProjectStages()
        {
            return _ReportRepository.FindProjectStage(projectStage => true);
        }

        /// <summary>
        /// Gets all loan places.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ILookUpEntity> GetAllLoanPlaces()
        {
            return _ReportRepository.FindLoanPlaces(loanPlace => true);
        }

        /// <summary>
        /// Gets all loan legal forms.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ILookUpEntity> GetAllLoanLegalForms()
        {
            return _ReportRepository.FindLoanLegalForms(loanLegalForm => true);
        }

        /// <summary>
        /// Gets all loan steps.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<IwfStep> GetAllLoanSteps()
        {
            return _ReportRepository.FindLoanSteps(loanLegalForm => true);
        }

        /// <summary>
        /// Gets all committees.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ICommittee> GetAllCommittees()
        {
            return _ReportRepository.FindCommittee(committee => true);
        }

        /// <summary>
        /// Gets all responsibles.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<IUser> GetAllResponsibles()
        {
            return _ReportRepository.FindUsers(users => true);
        }

        /// <summary>
        /// Gets all loan types.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ILookUpEntity> GetAllLoanTypes()
        {
            return _ReportRepository.FindLoanTypes(loanType => true);
        }

        /// <summary>
        /// Finds the total system report.
        /// </summary>
        /// <param name="customerType">Type of the customer.</param>
        /// <param name="educationLevelID">The education level ID.</param>
        /// <param name="selectedProjects">The selected projects.</param>
        /// <param name="projectStageID">The project stage ID.</param>
        /// <param name="socialStatusID">The social status ID.</param>
        /// <param name="ageFrom">The age from.</param>
        /// <param name="ageTo">The age to.</param>
        /// <param name="job">The job.</param>
        /// <param name="familyMembersFrom">The family members from.</param>
        /// <param name="familyMembersTo">The family members to.</param>
        /// <param name="familyMonthlyIncomeRangeFrom">The family monthly income range from.</param>
        /// <param name="familyMonthlyIncomeRangeTo">The family monthly income range to.</param>
        /// <param name="loanTypeID">The loan type ID.</param>
        /// <param name="loanStatus">The loan status.</param>
        /// <param name="legalFormID">The legal form ID.</param>
        /// <param name="loanPlaceTypeID">The loan place type ID.</param>
        /// <param name="currentLoanStepID">The current loan step ID.</param>
        /// <param name="paymentPeriod">The payment period.</param>
        /// <param name="loanExecutionAmount">The loan execution amount.</param>
        /// <param name="loanExecutionDateFrom">The loan execution date from.</param>
        /// <param name="loanExecutionDateTo">The loan execution date to.</param>
        /// <param name="loanLastInstallmentDateFrom">The loan last installment date from.</param>
        /// <param name="loanLastInstallmentDateTo">The loan last installment date to.</param>
        /// <param name="loanCreateDateFrom">The loan create date from.</param>
        /// <param name="loanCreateDateTo">The loan create date to.</param>
        /// <param name="refuseReason">The refuse reason.</param>
        /// <param name="boardDateFrom">The board date from.</param>
        /// <param name="boardDateTo">The board date to.</param>
        /// <param name="boardNumberFrom">The board number from.</param>
        /// <param name="boardNumberTo">The board number to.</param>
        /// <param name="boardDecision">The board decision.</param>
        /// <param name="postPoneReason">The post pone reason.</param>
        /// <param name="selectedCommittees">The selected committees.</param>
        /// <param name="responsibleID">The responsible ID.</param>
        /// <returns></returns>
        public IEnumerable<IVTotalSystemReport> FindTotalSystemReport(int? customerType,
                                            Guid educationLevelID,
                                            List<string> selectedProjects,
                                            Guid projectStageID,
                                            Guid socialStatusID,
                                            int ageFrom,
                                            int ageTo,
                                            string job,
                                            int familyMembersFrom,
                                            int familyMembersTo,
                                            int familyMonthlyIncomeRangeFrom,
                                            int familyMonthlyIncomeRangeTo,
                                            Guid loanTypeID,
                                            bool? loanStatus,
                                            Guid legalFormID,
                                            Guid loanPlaceTypeID,
                                            Guid currentLoanStepID,
                                            int? paymentPeriod,
                                            int? loanExecutionAmount,
                                            DateTime? loanExecutionDateFrom,
                                            DateTime? loanExecutionDateTo,
                                            DateTime? loanLastInstallmentDateFrom,
                                            DateTime? loanLastInstallmentDateTo,
                                            DateTime? loanCreateDateFrom,
                                            DateTime? loanCreateDateTo,
                                            string refuseReason,
                                            DateTime? boardDateFrom,
                                            DateTime? boardDateTo,
                                            int boardNumberFrom,
                                            int boardNumberTo,
                                            string boardDecision,
                                            string postPoneReason,
                                            List<string> selectedCommittees,
                                            Guid responsibleID)
        {

            List<Guid> porjectsIDs = new List<Guid>();
            List<Guid> committeesIDs = new List<Guid>();

            foreach (string projectID in selectedProjects)
            {
                porjectsIDs.Add(new Guid(projectID));
            }

            foreach (string committeeID in selectedCommittees)
            {
                committeesIDs.Add(new Guid(committeeID));
            }

            if (loanExecutionDateFrom == DateTime.MinValue)
                loanExecutionDateFrom = null;

            if (loanExecutionDateTo == DateTime.MinValue)
                loanExecutionDateTo = null;

            if (loanLastInstallmentDateFrom == DateTime.MinValue)
                loanLastInstallmentDateFrom = null;

            if (loanLastInstallmentDateTo == DateTime.MinValue)
                loanLastInstallmentDateTo = null;

            if (loanCreateDateFrom == DateTime.MinValue)
                loanCreateDateFrom = null;

            if (loanCreateDateTo == DateTime.MinValue)
                loanCreateDateTo = null;

            if (boardDateFrom == DateTime.MinValue)
                boardDateFrom = null;

            if (boardDateTo == DateTime.MinValue)
                boardDateTo = null;

            return _ReportRepository.FindTotalSystemReport(u => (porjectsIDs.Contains(u.ProjectID) || porjectsIDs.Count == 0)
                                           && (u.EducationalLevelID == educationLevelID || educationLevelID == Guid.Empty)
                                           && (u.CustomerType == customerType || !customerType.HasValue)
                                           && (u.StepID == projectStageID || projectStageID == Guid.Empty)
                                           && (u.SocialStatusID == socialStatusID || socialStatusID == Guid.Empty)
                                           && u.CustomerAge >= ageFrom
                                           && u.CustomerAge <= ageTo
                                           && (u.CurrentJob.Contains(job) || string.IsNullOrEmpty(job))
                                           && u.FamilyNumbers >= familyMembersFrom
                                           && u.FamilyNumbers <= familyMembersTo
                                           && u.IncomeAmount >= familyMonthlyIncomeRangeFrom
                                           && u.IncomeAmount <= familyMonthlyIncomeRangeTo
                                           && (u.LoanTypeID == loanTypeID || loanTypeID == Guid.Empty)
                                           && (u.LoanStatus == loanStatus || !loanStatus.HasValue)
                                           && (u.LegalFormID == legalFormID || legalFormID == Guid.Empty)
                                           && (u.LoanPlaceTypeID == loanPlaceTypeID || loanPlaceTypeID == Guid.Empty)
                                           && (u.LoanCurrentStep == currentLoanStepID || currentLoanStepID == Guid.Empty)
                                           && (u.LoanPaymentPeriod == paymentPeriod || !paymentPeriod.HasValue)
                                           && (u.ExecutionAmount == loanExecutionAmount || !loanExecutionAmount.HasValue)
                                           && (u.ExecutionDate >= loanExecutionDateFrom || !loanExecutionDateFrom.HasValue)
                                           && (u.ExecutionDate <= loanExecutionDateTo || !loanExecutionDateTo.HasValue)
                                           && (u.LastInstallmentDate >= loanLastInstallmentDateFrom || !loanLastInstallmentDateFrom.HasValue)
                                           && (u.LastInstallmentDate <= loanLastInstallmentDateTo || !loanLastInstallmentDateTo.HasValue)
                                           && (u.LoanCreatedDate >= loanCreateDateFrom || !loanCreateDateFrom.HasValue)
                                           && (u.LoanCreatedDate <= loanCreateDateTo || !loanCreateDateTo.HasValue)
                                           && ((u.Comments.Contains(refuseReason) && (u.ActionTypeID == new Guid("E8BB62BF-44FD-2FA8-D684-9BB39D27C2A4") || u.StepID == new Guid("0969F081-8A2D-CFCC-26BB-9E969311BBF7"))) || string.IsNullOrEmpty(refuseReason))
                                           && (u.BoardDate >= boardDateFrom || !boardDateFrom.HasValue)
                                           && (u.BoardDate <= boardDateTo || !boardDateTo.HasValue)
                                           && u.BoardNumber >= boardNumberFrom
                                           && u.BoardNumber <= boardNumberTo
                                           && (u.BoardDecision.Contains(boardDecision) || string.IsNullOrEmpty(boardDecision))
                                           && ((u.Comments.Contains(postPoneReason) && (u.StepID == new Guid("60C356A0-A65D-804A-6F03-D40A55D6150F") || u.StepID == new Guid("9733BEFA-94FF-9C85-9035-DA3CD6054D67"))) || string.IsNullOrEmpty(postPoneReason))
                                           && (committeesIDs.Contains(u.CommitteeID) || committeesIDs.Count == 0)
                                           && (u.ResponsibleID == responsibleID || responsibleID == Guid.Empty)
                                           ).ToList();
        }
    }
}